package com.jilefojing.controller;

import com.google.common.collect.Maps;
import com.jilefojing.base.BaseWeb;
import com.jilefojing.beans.data.request.base.RequestData;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.Statistics;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBook;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBookChapter;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.service.*;
import com.jilefojing.util.HtmlToJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("book")
public class BookController extends BaseWeb {
    @Autowired
    private IEBookService eBookService;
    @Autowired
    private IStatisticsService statisticsService;
    @Autowired
    private IRCategorySourceService rCategorySourceService;
    @Autowired
    private IDCategoryService dCategoryService;
    @Autowired
    private IEBookChapterService eBookChapterService;

    @PostMapping("list")
    public ResponseData list() {
        ResponseData responseData = new ResponseData();

        EnhancedEBook qldaj_enhancedEBook = new EnhancedEBook();
        qldaj_enhancedEBook.setId(1L);
        qldaj_enhancedEBook = (EnhancedEBook) eBookService.appendEnhancedCommons(qldaj_enhancedEBook, null);

        Map<String, Object> result = Maps.newHashMap();

        result.put("catelog_eBooks", qldaj_enhancedEBook.getEnhancedChildren());

        responseData.setData(result);

        return responseData;
    }

    @RequestMapping("catalogs")
    public ResponseData catalogs(@RequestBody RequestData requestData) {
        ResponseData responseData = new ResponseData();

        Long id = requestData.getRequest().getLong("id");

        EnhancedEBook enhancedEBook = (EnhancedEBook) eBookService.getEnhanced(id, null);
        enhancedEBook = (EnhancedEBook) eBookService.appendEnhancedCommons(enhancedEBook, null);

        Map<String, Object> result = Maps.newHashMap();

        result.put("eBooks", enhancedEBook.getEnhancedChildren());

        statisticsService.updateCount(ItemTypeEnumForSource.BOOK, id, Statistics.COLUMN_COUNT_WATCH, true, 1, null);

        responseData.setData(result);

        return responseData;
    }


    @RequestMapping("subCatalogs")
    public ResponseData getSubCatalogs(@RequestBody RequestData requestData) {
        ResponseData responseData = new ResponseData();

        Long id = requestData.getRequest().getLong("id");

        Pagination<EnhancedEBookChapter> pagination = new Pagination<EnhancedEBookChapter>(1, Integer.MAX_VALUE, true);
        pagination = eBookChapterService.getPagination(id, pagination, null);

        Map<String, Object> result = Maps.newHashMap();

        result.put("eBookChapterpagination", pagination);

        statisticsService.updateCount(ItemTypeEnumForSource.BOOK, id, Statistics.COLUMN_COUNT_WATCH, true, 1, null);

        responseData.setData(result);

        return responseData;
    }

    @RequestMapping("chapter")
    public ResponseData getChapter(@RequestBody RequestData requestData) {
        ResponseData responseData = new ResponseData();

        Long chapterId = requestData.getRequest().getLong("id");

        EnhancedEBookChapter enhancedEBookChapter = (EnhancedEBookChapter) eBookChapterService.getEnhanced(chapterId, null);

        //后端解析  不设置前端解析  如果开启了后端解析，前端一样需要改成后端解析配置 搜索关键字 后端解析
        String resetContent = this.getResetContent(enhancedEBookChapter.getContent());
        enhancedEBookChapter.setContent(resetContent);


        Map<String, Object> result = Maps.newHashMap();

        result.put("chapter", enhancedEBookChapter);

        statisticsService.updateCount(ItemTypeEnumForSource.BOOK_CHAPTER, chapterId, Statistics.COLUMN_COUNT_WATCH, true, 1, null);

        responseData.setData(result);

        return responseData;
    }

    private String getResetContent(String content) {
        String result = content;

        try {
            HtmlToJsonUtil.Params params = new HtmlToJsonUtil.Params();
            params.setType("html");
            result = HtmlToJsonUtil.by(content, params).get();
        } catch (Exception ex) {
            //忽略异常
        }

        return result;
    }

}
